﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;


namespace BasicDataManagement
{
    /// <summary>
    /// AddComboItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddComboItemWindow : Window
    {
        public ObservableCollection<string> Cats { get; set; }
        public string DefaultCat { get; set; }

        IOpertation op = OperationFactory.Create();
        ObservableCollection<ComboComponent> ComponentList;

        public ComboObject comboObject { get; set; }

        public AddComboItemWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (comboObject == null)
            {
                comboObject = new ComboObject();
            }

            nameTB.Text = comboObject.ObjectName;
            unitTB.Text = comboObject.Unit;

            catListCombo.ItemsSource = Cats;
            catListCombo.SelectedIndex = Cats.IndexOf(comboObject.BelongedCategoryName);

            if (comboObject.PriceType == PriceType.FixedPrice)
            {
                differentPriceRB.IsChecked = false;
                fixedPriceRB.IsChecked = true;
            }
            else
            {
                fixedPriceRB.IsChecked = false;
                differentPriceRB.IsChecked = true;
            }
            priceTB.Text = comboObject.Price.ToString();

            ComponentList = new ObservableCollection<ComboComponent>();
            foreach (var comp in comboObject.ComponentList)
            {
                ComboComponent cc = new ComboComponent(comp.ComponentName);
                cc.Ingredient = new ObservableCollection<Ingredient>(comp.IngredientList);
                ComponentList.Add(cc);
            }

            treeComponent.Items.Clear();
            treeComponent.ItemsSource = ComponentList;

            //listViewIngredient.ItemsSource = currentComponent;
        }

        private void btnAddComponent_Click(object sender, RoutedEventArgs e)
        {
            AddNewCatWindow win = new AddNewCatWindow();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                ComponentList.Add(new ComboComponent(win.CatName));
            }
        }

        private void tVIComponent_Click(object sender, MouseButtonEventArgs e)
        {
            foreach (var t in ComponentList)
            {
                if (t.ComponentName.Equals((e.Source as TextBlock).Text))
                {
                    //currentComponent = t.Ingredient;
                    listViewIngredient.ItemsSource = t.Ingredient;
                    btnAddIngredient.IsEnabled = true;
                    return;
                }
            }
            listViewIngredient.ItemsSource = null;
            btnAddIngredient.IsEnabled = false;
        }

        private void btnAddIngredient_Click(object sender, RoutedEventArgs e)
        {
            //AddIngredientWindow win = new AddIngredientWindow();
            //win.Cats = new ObservableCollection<string>(op.LoadFixedDishItemName());
            //win.Owner = this;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //if (win.ShowDialog() == true)
            //{
            //    (listViewIngredient.ItemsSource as ObservableCollection<Ingredient>).Add(win.IngredientResult);

            //    //currentComponent.Add(win.IngredientResult);
            //}
            AddSubItemWindow win = new AddSubItemWindow();
            win.ingredientList = op.LoadFixedDishSimpleObjects();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                (listViewIngredient.ItemsSource as ObservableCollection<Ingredient>).Add(win.IngredientResult);
            }
        }

        private void btnDelIngredient_Click(object sender, RoutedEventArgs e)
        {
            if (listViewIngredient.SelectedItem != null)
            {
                (listViewIngredient.ItemsSource as ObservableCollection<Ingredient>).Remove(listViewIngredient.SelectedItem as Ingredient);
                listViewIngredient.SelectedItem = null;
            }
        }


        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            comboObject.ObjectName = nameTB.Text;
            comboObject.Unit = unitTB.Text;
            comboObject.BelongedCategoryName = catListCombo.Text;
            comboObject.ComponentList = new List<ComboComponentObject>();
            if (fixedPriceRB.IsChecked == true)
            {
                comboObject.PriceType = PriceType.FixedPrice;
            }
            else
            {
                comboObject.PriceType = PriceType.DifferentPrice;
            }

            try
            {
                comboObject.Price = Convert.ToDecimal(priceTB.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("价格输入格式有误，请重新输入");
            }
            foreach (var component in this.ComponentList)
            {
                ComboComponentObject cco = new ComboComponentObject(component.ComponentName);
                cco.IngredientList = component.Ingredient.ToList();
                comboObject.ComponentList.Add(cco);
            }

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }


    }
}
